package com.wellsoft.mobileoa.utils.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;

import com.wellsoft.mobileoa.FuncPageActivity;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.utils.view.DataView;
import com.wellsoft.mobileoa.utils.view.PostFailedView;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpPost_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpPost_Response;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.ServiceResponse;

public class HttpPostAction extends BaseAction {

	public HttpPostAction(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		if (!(baseView.getClass().getSimpleName().equals(DataView.class.getSimpleName()) && SystemConfig.stringIsNullOrEmpty(action.getListfield()))) {
			ServiceResponse response = baseView.checkValid();
			if (!response.getExeFlag()) {
				return response;
			}
		}
		if (action.getPageConfirm() != null
				&& !SystemConfig.stringIsNullOrEmpty(action.getPageConfirm()
						.getMessage())) {
			PopConfirmDialog confirmDialog = new PopConfirmDialog(baseView.getBaseActivity(),
					action.getPageConfirm().getMessage());
			if (confirmDialog.showDialog() != PopConfirmDialog.DialogResultOK) {
				return new ServiceResponse(false);
			}
		}
		if (!SystemConfig.stringIsNullOrEmpty(action.getGoBackUrl())) {
			SystemConfig.setGoBackUrl(action.getGoBackUrl());
			SystemConfig.setGoBackTargetUrl(baseView.getUrl());
		}

		WSOA_HttpPost_Request request = new WSOA_HttpPost_Request();
		request.setUrl(action.getUrl());
		request.setCookies(SystemConfig.getCookies());
		request.setPostFields(new ArrayList<WSOA_PostField>());
		List<WSOA_PostField> viewValues = baseView.getValue();
		if (viewValues != null) {
			for (WSOA_PostField postField : viewValues) {
				if (postField != null) {
					if (SystemConfig.stringIsNullOrEmpty(postField
							.getFieldName())
							&& !SystemConfig.stringIsNullOrEmpty(action
									.getListfield())) {
						postField.setFieldName(action.getListfield());
						request.getPostFields().add(postField);

					} else if(!SystemConfig.stringIsNullOrEmpty(postField
							.getFieldName())){
						request.getPostFields().add(postField);
					}
				}
			}
		}
		if (action.getArguements() != null) {
			for (WSOA_PostField postField : action.getArguements()) {
				request.getPostFields().add(postField);
			}
		}

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);
		ServiceResponse httpPostResponse = SystemConfig.getService()
				.Callservice(httpRequests, "WSOA_HttpPost",
						WSOA_HttpPost_Response.class);
		if (!httpPostResponse.getExeFlag()) {
			return httpPostResponse;
		}
		WSOA_HttpPost_Response response = (WSOA_HttpPost_Response) httpPostResponse
				.getResponseData();
		if (response.getExeFlag() == 2) {
			if (SystemConfig.reLogin()) {
				return excuteAction();
			} else {
				BaseActivity.returnLoginActivity();
				return new ServiceResponse(false);
			}
		}
		if (response.getExeFlag() == 0) {
			if (response.getPageFields() == null
					|| response.getPageFields().size() == 0) {
				return new ServiceResponse(false, response.getPostRespPrompt()
						.getMessage());
			} else {
				WSOA_Page hiddenPage = new WSOA_Page();
				hiddenPage.setPageType("fail");
				hiddenPage.setPageFields(response.getPageFields());
				hiddenPage.setActions(response.getActions());
				PostFailedView postFailedView = new PostFailedView(baseView.getBaseActivity(), hiddenPage, null);

				return BaseAction.executeActions(response.getNextActionID(),
						response.getNextAction(), postFailedView);
			}
		}
		if (response.getNextPage() == null) {
			if (response.getPostRespPrompt() != null) {
				baseView.getBaseActivity().showMessage(response.getPostRespPrompt()
						.getMessage());
			}
			return BaseAction.executeActions(response.getNextActionID(),
					response.getNextAction(), baseView);
		} else {
			Intent intent = new Intent();
			intent.setClass(baseView.getBaseActivity(), FuncPageActivity.class);

			intent.putExtra("page", response.getNextPage());
			intent.putExtra("url", action.getUrl());
			baseView.getBaseActivity().NewActivity(intent);
		}

		return new ServiceResponse(true);
	}
}
